using System.Collections.Generic;
using System.Threading.Tasks;
using CPlatePrintSys.Domain.Models;
using CPlatePrintSys.Infrastructure.Repositories;

namespace CPlatePrintSys.Application.Services
{
    public interface IShipConfigService
    {
        Task<List<MotherShip>> GetAllMotherShipsAsync();
        Task AddMotherShipAsync(string shipNo);
        Task DeleteMotherShipAsync(int id);
        Task AddStreamShipAsync(string shipNo, int motherShipId);
        Task DeleteStreamShipAsync(int id);
        Task UpdateStreamShipAsync(int streamShipId, int newMotherShipId);
        Task UpdateMotherShipNoAsync(int id, string newShipNo);
        Task UpdateStreamShipNoAsync(int id, string newShipNo);
    }

    public class ShipConfigService : IShipConfigService
    {
        private readonly IShipRepository _repo;
        private readonly DataSyncService _dataSyncService;
        
        public ShipConfigService(IShipRepository repo, DataSyncService dataSyncService) 
        { 
            _repo = repo; 
            _dataSyncService = dataSyncService;
        }

        public Task<List<MotherShip>> GetAllMotherShipsAsync() => _repo.GetAllMotherShipsAsync();

        public async Task AddMotherShipAsync(string shipNo)
        {
            if (await _repo.MotherShipNoExistsAsync(shipNo))
                throw new System.Exception("母船号已存在");
            await _repo.AddMotherShipAsync(new MotherShip { ShipNo = shipNo });
        }

        public async Task DeleteMotherShipAsync(int id)
        {
            // 先获取母船号，用于删除相关标注信息
            var motherShip = await _repo.GetMotherShipByIdAsync(id);
            if (motherShip != null)
            {
                // 删除相关标注信息
                await _dataSyncService.DeleteAnnotationsByMotherShipNoAsync(motherShip.ShipNo);
                // 删除母船
                await _repo.DeleteMotherShipAsync(id);
            }
        }

        public async Task AddStreamShipAsync(string shipNo, int motherShipId)
        {
            if (await _repo.StreamShipNoExistsAsync(shipNo))
                throw new System.Exception("流用船号已存在");
            await _repo.AddStreamShipAsync(new StreamShip { ShipNo = shipNo, MotherShipId = motherShipId });
        }

        public async Task DeleteStreamShipAsync(int id)
        {
            // 先获取流用船号，用于删除相关标注信息
            var streamShip = await _repo.GetStreamShipByIdAsync(id);
            if (streamShip != null)
            {
                // 删除相关标注信息
                await _dataSyncService.DeleteAnnotationsByStreamShipNoAsync(streamShip.ShipNo);
                // 删除流用船
                await _repo.DeleteStreamShipAsync(id);
            }
        }

        public Task UpdateStreamShipAsync(int streamShipId, int newMotherShipId)
            => _repo.UpdateStreamShipAsync(streamShipId, newMotherShipId);

        public async Task UpdateMotherShipNoAsync(int id, string newShipNo)
        {
            // 先获取旧船号
            var motherShip = await _repo.GetMotherShipByIdAsync(id);
            if (motherShip != null)
            {
                var oldShipNo = motherShip.ShipNo;
                // 更新母船号
                await _repo.UpdateMotherShipNoAsync(id, newShipNo);
                // 同步更新相关标注信息
                await _dataSyncService.UpdateMotherShipNoInAnnotationsAsync(oldShipNo, newShipNo);
            }
        }

        public async Task UpdateStreamShipNoAsync(int id, string newShipNo)
        {
            // 先获取旧船号
            var streamShip = await _repo.GetStreamShipByIdAsync(id);
            if (streamShip != null)
            {
                var oldShipNo = streamShip.ShipNo;
                // 更新流用船号
                await _repo.UpdateStreamShipNoAsync(id, newShipNo);
                // 同步更新相关标注信息
                await _dataSyncService.UpdateStreamShipNoInAnnotationsAsync(oldShipNo, newShipNo);
            }
        }
    }
} 